<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="报销编号" prop="feeNo">
        <el-input v-model="queryParams.feeNo" placeholder="请输入报销编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="报销名称" prop="feeName">
        <el-input
          v-model="queryParams.feeName"
          placeholder="请输入报销名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="报销时间">
        <el-date-picker v-model="dateRange" value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px"
          :default-time="['00:00:00', '23:59:59']"></el-date-picker>
      </el-form-item>
      <el-form-item label="报销金额">
        <el-input-number v-model="startTotalFee" controls-position="right" /> -
        <el-input-number controls-position="right" v-model="endTotalFee" :min="startTotalFee ? startTotalFee + 1 : 2"
          :max="31" />
      </el-form-item>
      <el-form-item label="填报人" prop="applyUserId">
        <UserDialog placeholder="请输入填报人" v-model="queryParams.applyUserId"></UserDialog>
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <el-cascader :props="{ value: 'id', checkStrictly: true }" :show-all-levels="false" placeholder="请选择部门"
          v-model="deptId" :options="deptOptions"></el-cascader>
      </el-form-item>
      <el-form-item label="流程状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择流程状态">
          <el-option label="已完成" value="end" />
          <el-option label="审核中" value="audit" />
          <el-option label="已驳回" value="refuse" />
          <el-option label="已作废" value="cancel" />
          <el-option label="草稿" value="draft" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="printBoxRecored"
          v-hasPermi="['bxgl:bxgl:prints']">打印</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['bxgl:bxgl:export']">导出</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:user:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:user:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:user:remove']"
          >删除</el-button
        >
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table @selection-change="handleSelectionChange" fit v-loading="loading" border :data="dataList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column show-overflow-tooltip label="流程状态" width="100">
        <template slot-scope="scope">
          <el-tag size="mini">{{ scope.row.statusName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="报销编号" prop="feeNo" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link @click="handleDetail(scope.row)" type="primary">{{
            scope.row.feeNo
          }}</el-link>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="报销名称"
        prop="feeName"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-link type="primary" @click="handleDetail(scope.row)">{{
            scope.row.feeName
          }}</el-link>
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        label="所属项目"
        prop="projectName"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <el-link type="primary" @click="handleDetail(scope.row)">{{
            scope.row.projectName
          }}</el-link>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="日期" prop="happenDate" /> -->
      <el-table-column label="本次报销金额" prop="totalFee" />
      <el-table-column label="部门" prop="deptName" />
      <el-table-column label="填报人" prop="applyUserName">
        <template slot-scope="scope">
          {{ scope.row.applyUserName || "--" }}
        </template>
      </el-table-column>
      <el-table-column label="报销类型">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_fee_type" :value="scope.row.payTypeValue" />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" v-hasPermi="['bxgl:bxgl:info']"
            @click="handleDetail(scope.row)">详情</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            v-if="scope.row.status === 'refuse'"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bxgl:bxgl:edit']"
            >修改</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 打印模板位置 -->
    <PrintBox :isPageSize="false" :printRow="printList" id="printBoxsLl">
      <template slot="title" slot-scope="scope">
        费用报销审批单
      </template>
      <template slot-scope="scope">
        <div class="desc" style="display: flex ;align-items: center;">
          <div style="width: 280px;text-align: left;">报销部门: {{ scope.item.deptName }}</div>
          <div style="width: 50px;text-align: right;">{{ scope.item.createTime && scope.item.createTime.substr(0, 4) }}年
          </div>
          <div style="width: 50px;text-align: right;">{{ scope.item.createTime && scope.item.createTime.substr(5, 2) }}月
          </div>
          <div style="width: 50px;text-align: right;">{{ scope.item.createTime && scope.item.createTime.substr(8, 2) }}日填
          </div>
          <div style="width: 50px;text-align: right;">{{ scope.item.payTypeValue == 0 ? '现金' : '对公' }}</div>
          <div style="width: 250px;text-align: right;">单据及附件共</div>
          <div style="width: 100px;text-align: right;"> <span style="margin-right: 30px;">{{ scope.item.oss.length
          }}</span> 页</div>
        </div>
        <!-- 表格  class  table-page  必须-->
        <table style="border-collapse: collapse" border="1" cellspacing="0" class="table-page wasd" :isVertical="false">
          <tr>
            <td colspan="4" style="width: 80mm">用途</td>
            <td colspan="3" style="width: 30mm">金额（元）</td>
            <td rowspan="3" colspan="1" style="width: 10mm">备注</td>

            <td rowspan="3" colspan="4" style="width: 80mm">
            </td>
          </tr>
          <!-- {{ scope.item.details }} -->
          <tr v-for="(item, index) in scope.item.details ">
            <td colspan="4" style="width: 30mm">{{ item.subjectValue }}</td>
            <td colspan="3" style="width: 30mm">{{ item.fee }}</td>
            <td rowspan="7" colspan="1" style="width: 10mm" v-if="index == 2">领导审批</td>

          </tr>
         
          <tr v-if="scope.item.details && scope.item.details.length < 8"
            v-for="(item, index) in 8 - scope.item.details.length">
            <td colspan="4" style="width: 30mm"></td>
            <td colspan="3" style="width: 30mm"></td>
            <td rowspan="7" colspan="1" style="width: 10mm"
              v-if="scope.item.details && scope.item.details.length == 1 && index == 1">领导审批</td>
            <td rowspan="7" colspan="1" style="width: 10mm"
              v-if="scope.item.details && scope.item.details.length == 2 && index == 0">领导审批</td>
            <td v-if="scope.item.details && scope.item.details.length == 2 && index == 0" rowspan="4" colspan="4"
              style="width: 80mm"><template v-if="CheckApprove(scope, 'glcsp_2').ShowImg">
                <img style="width: 80px; " :src="CheckApprove(scope, 'glcsp_2').ApproveImg" />
              </template></td>
            <td v-if="scope.item.details && scope.item.details.length == 1 && index == 1" rowspan="7" colspan="4"
              style="width: 80mm">
              <template v-if="CheckApprove(scope, 'glcsp_2').ShowImg">
                <img style="width: 80px; " :src="CheckApprove(scope, 'glcsp_2').ApproveImg" />
              </template>
            </td>
          </tr>
           <tr>
            <td colspan="4" style="width: 30mm">合计</td>
            <td colspan="3" style="width: 30mm">{{ scope.item.totalFee }}</td>
          </tr>
          <tr>
            <td colspan="7" style="text-align: left;">金额（大写）{{ scope.item.chineseFee }}</td>
            <td colspan="3" style="width: 30mm"> <span style="margin-right: 100px;">原借款</span> <span>元</span> </td>
            <td colspan="4" style="width: 40mm"><span style="margin-right: 50px;">应退余款</span> <span>元</span> </td>

          </tr>
        </table>
        <!-- 底部信息位置 -->
        <div class="desc" style="display: flex;align-items: center;">
          <div style="width: 150px;display: flex;align-items: center;">会计审核:
            <template v-if="CheckApprove(scope, 'kjsh').ShowImg">
              <img style="width: 80px; " :src="CheckApprove(scope, 'kjsh').ApproveImg" />
            </template>
          </div>
          <div style="width: 230px;display: flex;align-items: center;">复核：
            <template v-if="CheckApprove(scope, 'glcsp_1').ShowImg">
              <img style="width: 80px; " :src="CheckApprove(scope, 'glcsp_1').ApproveImg" />
            </template>
          </div>
          <div style="width: 150px;display: flex;align-items: center;">部门负责人：<template
              v-if="CheckApprove(scope, 'deptLeader').ShowImg">
              <img style="width: 50px; " :src="CheckApprove(scope, 'deptLeader').ApproveImg" />
            </template>
          </div>
          <div style="width: 150px;display: flex;align-items: center;">填报人:
            <img style="width: 50px;  object-fit: cover" :src="$VUE_APP_IMG_API + scope.item.esign" />
          </div>
          <div>领款人: <span style="margin-left: 20px;">{{ scope.item.receiverUserName }}</span> </div>
        </div>
      </template>
    </PrintBox>
  </div>
</template>

<script>
import { listFee, delFee } from "@/api/finance/bxgl/index.js";
import { userSelect, deptTreeSelect } from "@/api/system/user.js";
import printStyle from "@/utils/print.js";
export default {
  name: "Bxgl",
  dicts: ["sys_comp_fee_status", "sys_fee_type"],

  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        feeNo: undefined,
        feeName: undefined,
        projectName: undefined,
        status: ''
      },
      createByName: "",
      // 日期范围
      dateRange: [],
      startTotalFee: undefined,
      endTotalFee: undefined,
      userList: [],
      deptOptions: [],
      deptId: [],
      printList: [],
      infos: [],
    };
  },
  created() {
    const str = sessionStorage.getItem('queryParams');
    if (str) {
      this.queryParams = JSON.parse(str);
      if (this.queryParams.startTime) {
        this.dateRange.push(this.queryParams.startTime, this.queryParams.startTime)
      }
      setTimeout(() => {
        sessionStorage.removeItem('queryParams');
      }, 500)
    }
    this.getList();
    this.userSelect();
    this.getDeptTree();
  },
  methods: {
    changeUser(e) {
      console.log(e);
      this.queryParams.createByName = e.nickName;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.compFeeId);
      this.infos = selection;
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      this.queryParams.ids = this.ids.toString()
    },
    printBoxRecored() {
      if (this.infos.length) {
        this.printList = this.infos;
      } else {
        //反之打印当前整个列表
        this.printList = this.dataList;
      }
      console.log(this.printList, "this.printList");
      setTimeout(() => {
        this.$print({
          printable: "printBoxsLl", //打印区域id
          type: "html", //打印类型是html
          scanStyles: false,
          targetStyles: ["*"],
          style: this.workStyleLl(),
        });
      }, 500);
    },
    workStyleLl() {
      return `${printStyle}

      .desc{
        text-align: left; font-size: 12px; margin-top: 10px
      }
        table,
      table tr th,
      table tr td {
        border: 1px solid;
        height: 20px;
        padding: 5px;

      }
      tr {
        page-break-inside: avoid;
      }
      .table_header{
        font-size:16px;
      }
      `;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      const configId = row.compFeeId || this.ids;
      this.$router.push("./addBxgl?isEdit=true&id=" + configId);
    },
    handleDetail(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      // this.$router.push("./addBxgl?isInfo=true&id=" + row.compFeeId);
      this.$router.push({
        path: "/bxgl/process/detail/" + row.piid,
        query: {
          definitionId: null,
          deployId: null,
          taskId: null,
          finished: false,
          id: row.compFeeId,
        },
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.compFeeId || this.ids;

      this.$modal
        .confirm('是否确认删除ID为"' + configId + '"的数据项？')
        .then(function () {
          return delFee(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    handleAdd() {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      this.$router.push("./addBxgl");
    },
    userSelect() {
      userSelect().then((response) => {
        this.userList = response.data;
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.startTotalFee = this.startTotalFee;
      this.queryParams.endTotalFee = this.endTotalFee;
      this.queryParams.deptId = this.deptId[this.deptId.length - 1];

      listFee(
        this.addDateRangeTime(
          this.queryParams,
          this.dateRange,
          "startTime",
          "endTime"
        )
      ).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.deptId = [];
      this.startTotalFee = undefined;
      this.endTotalFee = undefined;
      this.resetForm("queryForm");
      this.createByName = "";
      this.handleQuery();
    },
    handleExport() {
      this.download(
        "system/fee/export",
        {
          ...this.queryParams,
        },
        `报销记录表_${this.parseTime(new Date().getTime(), "{y}-{m}-{d}")}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}
</style>
